package jin.si.com.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jin.si.com.entity.Person;
import jin.si.com.entity.User;
import jin.si.com.service.UserService;
import jin.si.com.utils.Go;
import jin.si.com.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserController
 * @Author MT
 * @Date 2021/5/4 11:04
 **/

//@Controller
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService service;

    /**
     *
     * @return
     */

    @RequestMapping("getUsers")
    public List<User> getUsers(){
        List<User> list = service.getUsers();
        return list;
    }




    //GetMapping
    @GetMapping("getUsers1")
    public List<User> getUsers1(){
        List<User> list = service.getUsers();
        return list;
    }

    //PostMapping
    @PostMapping("getUsers2")
    public List<User> getUsers2(){
        List<User> list = service.getUsers();
        return list;
    }

    //PutMapping
    @PutMapping("getUsers3")
    public List<User> getUsers3(){
        List<User> list = service.getUsers();
        return list;
    }

    //DeleteMapping
    @DeleteMapping("getUsers4")
    public boolean getUsers4(){
        return true;
    }

    //SpringBoot2.X统⼀接⼝返回协议！掌握开发规范
    @GetMapping("getList")
    public JsonData getList(){
        List<User> list = service.getUsers();
        List<User> list2 = null;
        return list!=null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }


    //前台给后台传json格式，对象套list
    @PostMapping("getPerson")
    public JsonData getPerson(@RequestBody Person p){
        System.out.println(p);

        return JsonData.buildSuccess(true);
    }


    //json注解
    @GetMapping("getPerson1")
    public JsonData getPerson1(HttpSession s){
        List<Person> list=service.getPerson1();
        return JsonData.buildSuccess(list);
    }



    //序列化
    @GetMapping("getPerson2")
    public JsonData getPerson2() throws JsonProcessingException {
        List<Person> list=service.getPerson1();
        System.out.println(list);
        ObjectMapper objectMapper=new ObjectMapper();

        //序列化 将json对象转json字符串
        String str = objectMapper.writeValueAsString(list);
        System.out.println(str);

        //将json字符串转json对象
        List list1 = objectMapper.readValue(str, List.class);
        System.out.println(list1);

        return JsonData.buildSuccess(list);
    }


    //异常
    @RequestMapping("getExe")
    public JsonData getExe(){
        int  num=10/0;
        return JsonData.buildSuccess(true);
    }


    //login登录方法
    @RequestMapping("/loginUser")
    public JsonData loginUser(String name,String pwd){
       String token=service.loginUser(name,pwd);
        System.out.println(token);
        return token!=null?JsonData.buildSuccess(token):JsonData.buildError("信息为空");
    }



}
